home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / gdb-4.12 / sim / h8300 / compile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  31.9 KB  |  1,812 lines

  1. /*
  2.  * Simulator for the Hitachi H8/300 architecture.
  3.  *
  4.  * Written by Steve Chamberlain of Cygnus Support. sac@cygnus.com
  5.  *
  6.  * This file is part of H8/300 sim
  7.  *
  8.  *
  9.  * THIS SOFTWARE IS NOT COPYRIGHTED
  10.  *
  11.  * Cygnus offers the following for use in the public domain.  Cygnus makes no
  12.  * warranty with regard to the software or its performance and the user
  13.  * accepts the software "AS IS" with all faults.
  14.  *
  15.  * CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS
  16.  * SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #include <signal.h>
  21. #include <sys/times.h>
  22. #include <sys/param.h>
  23. #include "ansidecl.h"
  24. #include "sysdep.h"
  25. #include "remote-sim.h"
  26.  
  27. int debug;
  28.  
  29.  
  30. #define X(op, size)  op*4+size
  31.  
  32. #define SP (h8300hmode ? SL:SW)
  33. #define SB 0
  34. #define SW 1
  35. #define SL 2
  36. #define OP_REG 1
  37. #define OP_DEC 2
  38. #define OP_DISP 3
  39. #define OP_INC 4
  40. #define OP_PCREL 5
  41. #define OP_MEM 6
  42. #define OP_CCR 7
  43. #define OP_IMM 8
  44. #define OP_ABS 10
  45. #define h8_opcodes ops
  46. #define DEFINE_TABLE
  47. #include "opcode/h8300.h"
  48.  
  49. #include "inst.h"
  50.  
  51. #define LOW_BYTE(x) ((x) & 0xff)
  52. #define HIGH_BYTE(x) (((x)>>8) & 0xff)
  53. #define P(X,Y) ((X<<8) | Y)
  54.  
  55. #define BUILDSR()   cpu.ccr = (N << 3) | (Z << 2) | (V<<1) | C;
  56.  
  57. #define GETSR()            \
  58.   c = (cpu.ccr >> 0) & 1;\
  59.   v = (cpu.ccr >> 1) & 1;\
  60.   nz = !((cpu.ccr >> 2) & 1);\
  61.   n = (cpu.ccr >> 3) & 1;
  62.  
  63. #ifdef __CHAR_IS_SIGNED__
  64. #define SEXTCHAR(x) ((char)(x))
  65. #endif
  66.  
  67. #ifndef SEXTCHAR
  68. #define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff):x)
  69. #endif
  70.  
  71. #define UEXTCHAR(x) ((x) & 0xff)
  72. #define UEXTSHORT(x) ((x) & 0xffff)
  73. #define SEXTSHORT(x) ((short)(x))
  74.  
  75. static cpu_state_type cpu;
  76.  
  77. int h8300hmode = 0;
  78.  
  79.  
  80. static int
  81. get_now ()
  82. {
  83.   struct tms b;
  84.  
  85.   return time (0);
  86. #if 0
  87.   times (&b);
  88.   return b.tms_utime + b.tms_stime;
  89. #endif
  90. }
  91.  
  92. static int
  93. now_persec ()
  94. {
  95.   return 1;
  96. }
  97.  
  98.  
  99. static int
  100. bitfrom (x)
  101. {
  102.   switch (x & SIZE)
  103.     {
  104.     case L_8:
  105.       return SB;
  106.     case L_16:
  107.       return SW;
  108.     case L_32:
  109.       return SL;
  110.     case L_P:
  111.       return h8300hmode ? SL : SW;
  112.     }
  113. }
  114.  
  115. static
  116. unsigned int
  117. lvalue (x, rn)
  118. {
  119.   switch (x / 4)
  120.     {
  121.     case OP_DISP:
  122.       if (rn == 8)
  123.     {
  124.       return X (OP_IMM, SP);
  125.     }
  126.       return X (OP_REG, SP);
  127.  
  128.     case OP_MEM:
  129.  
  130.       return X (OP_MEM, SP);
  131.     default:
  132.       abort ();
  133.     }
  134. }
  135.  
  136. static unsigned int
  137. decode (addr, data, dst)
  138.      int addr;
  139.      unsigned char *data;
  140.      decoded_inst *dst;
  141.  
  142. {
  143.   int rs = 0;
  144.   int rd = 0;
  145.   int rdisp = 0;
  146.   int abs = 0;
  147.   int plen = 0;
  148.  
  149.   struct h8_opcode *q = h8_opcodes;
  150.   int size = 0;
  151.   dst->dst.type = -1;
  152.   dst->src.type = -1;
  153.   /* Find the exact opcode/arg combo */
  154.   while (q->name)
  155.     {
  156.       op_type *nib;
  157.       unsigned int len = 0;
  158.  
  159.       nib = q->data.nib;
  160.  
  161.       while (1)
  162.     {
  163.       op_type looking_for = *nib;
  164.       int thisnib = data[len >> 1];
  165.  
  166.       thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
  167.  
  168.       if (looking_for < 16)
  169.         {
  170.  
  171.           if (looking_for != thisnib)
  172.         goto fail;
  173.         }
  174.       else
  175.         {
  176.  
  177.           if ((int) looking_for & (int) B31)
  178.         {
  179.           if (!(((int) thisnib & 0x8) != 0))
  180.             goto fail;
  181.           looking_for = (op_type) ((int) looking_for & ~(int)
  182.                        B31);
  183.           thisnib &= 0x7;
  184.  
  185.         }
  186.           if ((int) looking_for & (int) B30)
  187.         {
  188.           if (!(((int) thisnib & 0x8) == 0))
  189.             goto fail;
  190.           looking_for = (op_type) ((int) looking_for & ~(int) B30);
  191.         }
  192.           if (looking_for & DBIT)
  193.         {
  194.           if ((looking_for & 5) != (thisnib & 5))
  195.             goto fail;
  196.           abs = (thisnib & 0x8) ? 2 : 1;
  197.         }
  198.           else if (looking_for & (REG | IND | INC | DEC))
  199.         {
  200.           if (looking_for & REG)
  201.             {
  202.               /*
  203.                * Can work out size from the
  204.                * register
  205.                */
  206.               size = bitfrom (looking_for);
  207.             }
  208.           if (looking_for & SRC)
  209.             {
  210.               rs = thisnib;
  211.             }
  212.           else
  213.             {
  214.               rd = thisnib;
  215.             }
  216.         }
  217.           else if (looking_for & L_16)
  218.         {
  219.           abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
  220.           plen = 16;
  221.           if (looking_for & (PCREL | DISP))
  222.             {
  223.               abs = (short) (abs);
  224.             }
  225.         }
  226.           else if (looking_for & ABSJMP)
  227.         {
  228.           abs =
  229.             (data[1] << 16)
  230.             | (data[2] << 8)
  231.             | (data[3]);
  232.         }
  233.           else if (looking_for & L_32)
  234.         {
  235.           int i = len >> 1;
  236.           abs = (data[i] << 24)
  237.             | (data[i + 1] << 16)
  238.             | (data[i + 2] << 8)
  239.             | (data[i + 3]);
  240.  
  241.           plen = 32;
  242.  
  243.         }
  244.           else if (looking_for & L_24)
  245.         {
  246.           int i = len >> 1;
  247.           abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i +
  248.                                      2]);
  249.           plen = 24;
  250.         }
  251.           else if (looking_for & IGNORE)
  252.         {
  253.  
  254.         }
  255.           else if (looking_for & DISPREG)
  256.         {
  257.           rdisp = thisnib & 0x7;
  258.         }
  259.           else if (looking_for & KBIT)
  260.         {
  261.           switch (thisnib)
  262.             {
  263.             case 9:
  264.               abs = 4;
  265.               break;
  266.             case 8:
  267.               abs = 2;
  268.               break;
  269.             case 0:
  270.               abs = 1;
  271.               break;
  272.             }
  273.         }
  274.           else if (looking_for & L_8)
  275.         {
  276.           plen = 8;
  277.  
  278.           if (looking_for & PCREL)
  279.             {
  280.               abs = SEXTCHAR (data[len >> 1]);
  281.             }
  282.           else
  283.             {
  284.               abs = data[len >> 1] & 0xff;
  285.             }
  286.         }
  287.           else if (looking_for & L_3)
  288.         {
  289.           plen = 3;
  290.  
  291.           abs = thisnib;
  292.         }
  293.           else if (looking_for == E)
  294.         {
  295.           dst->op = q;
  296.  
  297.           /* Fill in the args */
  298.           {
  299.             op_type *args = q->args.nib;
  300.             int hadone = 0;
  301.  
  302.  
  303.             while (*args != E)
  304.               {
  305.             int x = *args;
  306.             int rn = (x & DST) ? rd : rs;
  307.             ea_type *p;
  308.  
  309.             if (x & DST)
  310.               {
  311.                 p = &(dst->dst);
  312.               }
  313.             else
  314.               {
  315.                 p = &(dst->src);
  316.               }
  317.  
  318.  
  319.             if (x & (IMM | KBIT | DBIT))
  320.               {
  321.                 p->type = X (OP_IMM, size);
  322.                 p->literal = abs;
  323.               }
  324.             else if (x & REG)
  325.               {
  326.                 /*
  327.                    Reset the size, some
  328.                    ops (like mul) have two sizes */
  329.  
  330.                 size = bitfrom (x);
  331.                 p->type = X (OP_REG, size);
  332.                 p->reg = rn;
  333.               }
  334.             else if (x & INC)
  335.               {
  336.                 p->type = X (OP_INC, size);
  337.                 p->reg = rn & 0x7;
  338.               }
  339.             else if (x & DEC)
  340.               {
  341.                 p->type = X (OP_DEC, size);
  342.                 p->reg = rn & 0x7;
  343.               }
  344.             else if (x & IND)
  345.               {
  346.                 p->type = X (OP_DISP, size);
  347.                 p->reg = rn & 0x7;
  348.                 p->literal = 0;
  349.               }
  350.             else if (x & (ABS | ABSJMP | ABSMOV))
  351.               {
  352.                 p->type = X (OP_DISP, size);
  353.                 p->literal = abs;
  354.                 p->reg = 8;
  355.               }
  356.             else if (x & MEMIND)
  357.               {
  358.                 p->type = X (OP_MEM, size);
  359.                 p->literal = abs;
  360.               }
  361.             else if (x & PCREL)
  362.               {
  363.                 p->type = X (OP_PCREL, size);
  364.                 p->literal = abs + addr + 2;
  365.               }
  366.             else if (x & ABSJMP)
  367.               {
  368.                 p->type = X (OP_IMM, SP);
  369.                 p->literal = abs;
  370.               }
  371.             else if (x & DISP)
  372.               {
  373.                 p->type = X (OP_DISP, size);
  374.                 p->literal = abs;
  375.                 p->reg = rdisp & 0x7;
  376.               }
  377.             else if (x & CCR)
  378.               {
  379.                 p->type = OP_CCR;
  380.               }
  381.             else
  382.               printf ("Hmmmm %x", x);
  383.  
  384.  
  385.             args++;
  386.               }
  387.           }
  388.  
  389.           /*
  390.              * But a jmp or a jsr gets
  391.              * automagically lvalued, since we
  392.              * branch to their address not their
  393.              * contents
  394.            */
  395.           if (q->how == O (O_JSR, SB)
  396.               || q->how == O (O_JMP, SB))
  397.             {
  398.               dst->src.type = lvalue (dst->src.type, dst->src.reg);
  399.             }
  400.  
  401.  
  402.           if (dst->dst.type == -1)
  403.             dst->dst = dst->src;
  404.  
  405.           dst->opcode = q->how;
  406.           dst->cycles = q->time;
  407.  
  408.           /* And a jsr to 0xc4 is turned into a magic trap */
  409.  
  410.           if (dst->opcode == O (O_JSR, SB))
  411.             {
  412.               if (dst->src.literal == 0xc4)
  413.             {
  414.               dst->opcode = O (O_SYSCALL, SB);
  415.             }
  416.             }
  417.  
  418.           dst->next_pc = addr + len / 2;
  419.           return;
  420.         }
  421.           else
  422.         {
  423.           printf ("Dont understand %x \n", looking_for);
  424.         }
  425.         }
  426.  
  427.       len++;
  428.       nib++;
  429.     }
  430.  
  431.     fail:
  432.       q++;
  433.     }
  434.  
  435.   dst->opcode = O (O_ILL, SB);
  436. }
  437.  
  438.  
  439. static void
  440. compile (pc)
  441. {
  442.   int idx;
  443.  
  444.   /* find the next cache entry to use */
  445.  
  446.   idx = cpu.cache_top + 1;
  447.   cpu.compiles++;
  448.   if (idx >= cpu.csize)
  449.     {
  450.       idx = 1;
  451.     }
  452.   cpu.cache_top = idx;
  453.  
  454.   /* Throw away its old meaning */
  455.   cpu.cache_idx[cpu.cache[idx].oldpc] = 0;
  456.  
  457.   /* set to new address */
  458.   cpu.cache[idx].oldpc = pc;
  459.  
  460.   /* fill in instruction info */
  461.   decode (pc, cpu.memory + pc, cpu.cache + idx);
  462.  
  463.   /* point to new cache entry */
  464.   cpu.cache_idx[pc] = idx;
  465. }
  466.  
  467.  
  468. static unsigned char *breg[18];
  469. static unsigned short *wreg[18];
  470. static unsigned int *lreg[18];
  471.  
  472. #define GET_B_REG(x) *(breg[x])
  473. #define SET_B_REG(x,y) (*(breg[x])) = (y)
  474. #define GET_W_REG(x) *(wreg[x])
  475. #define SET_W_REG(x,y) (*(wreg[x])) = (y)
  476.  
  477. #define GET_L_REG(x) *(lreg[x])
  478. #define SET_L_REG(x,y) (*(lreg[x])) = (y)
  479.  
  480. #define GET_MEMORY_L(x) \
  481.   ((cpu.memory[x+0] << 24) | (cpu.memory[x+1] << 16) | (cpu.memory[x+2] << 8) | cpu.memory[x+3])
  482.  
  483. #define GET_MEMORY_W(x) \
  484.   ((cpu.memory[x+0] << 8) | (cpu.memory[x+1] << 0))
  485.  
  486.  
  487. #define SET_MEMORY_B(x,y) \
  488.   (cpu.memory[(x)] = y)
  489.  
  490. #define SET_MEMORY_W(x,y) \
  491. {register unsigned char *_p = cpu.memory+x;\
  492.    register int __y = y;\
  493.      _p[0] = (__y)>>8;\
  494.        _p[1] =(__y);     }
  495.  
  496. #define SET_MEMORY_L(x,y)  \
  497. {register unsigned char *_p = cpu.memory+x;register int __y = y;\
  498.    _p[0] = (__y)>>24;     _p[1] = (__y)>>16;     _p[2] = (__y)>>8;     _p[3] = (__y)>>0;}
  499.  
  500. #define GET_MEMORY_B(x)  (cpu.memory[x])
  501.  
  502. int
  503. fetch (arg, n)
  504.      ea_type *arg;
  505. {
  506.   int rn = arg->reg;
  507.   int abs = arg->literal;
  508.   int r;
  509.   int t;
  510.  
  511.   switch (arg->type)
  512.     {
  513.     case X (OP_REG, SB):
  514.       return GET_B_REG (rn);
  515.     case X (OP_REG, SW):
  516.       return GET_W_REG (rn);
  517.     case X (OP_REG, SL):
  518.       return GET_L_REG (rn);
  519.     case X (OP_IMM, SB):
  520.     case X (OP_IMM, SW):
  521.     case X (OP_IMM, SL):
  522.       return abs;
  523.     case X (OP_DEC, SB):
  524.       abort ();
  525.  
  526.     case X (OP_INC, SB):
  527.       t = GET_L_REG (rn);
  528.       t &= cpu.mask;
  529.       r = GET_MEMORY_B (t);
  530.       t++;
  531.       t = t & cpu.mask;
  532.       SET_L_REG (rn, t);
  533.       return r;
  534.       break;
  535.     case X (OP_INC, SW):
  536.       t = GET_L_REG (rn);
  537.       t &= cpu.mask;
  538.       r = GET_MEMORY_W (t);
  539.       t += 2;
  540.       t = t & cpu.mask;
  541.       SET_L_REG (rn, t);
  542.       return r;
  543.     case X (OP_INC, SL):
  544.       t = GET_L_REG (rn);
  545.       t &= cpu.mask;
  546.       r = GET_MEMORY_L (t);
  547.  
  548.       t += 4;
  549.       t = t & cpu.mask;
  550.       SET_L_REG (rn, t);
  551.       return r;
  552.  
  553.     case X (OP_DISP, SB):
  554.       t = GET_L_REG (rn) + abs;
  555.       t &= cpu.mask;
  556.       return GET_MEMORY_B (t);
  557.  
  558.     case X (OP_DISP, SW):
  559.       t = GET_L_REG (rn) + abs;
  560.       t &= cpu.mask;
  561.       return GET_MEMORY_W (t);
  562.  
  563.     case X (OP_DISP, SL):
  564.       t = GET_L_REG (rn) + abs;
  565.       t &= cpu.mask;
  566.       return GET_MEMORY_L (t);
  567.  
  568.     default:
  569.       abort ();
  570.  
  571.     }
  572. }
  573.  
  574.  
  575.  
  576.  
  577.  
  578. static
  579. void
  580. store (arg, n)
  581.      ea_type *arg;
  582.      int n;
  583. {
  584.   int rn = arg->reg;
  585.   int abs = arg->literal;
  586.   int t;
  587.  
  588.   switch (arg->type)
  589.     {
  590.     case X (OP_REG, SB):
  591.       SET_B_REG (rn, n);
  592.       break;
  593.     case X (OP_REG, SW):
  594.       SET_W_REG (rn, n);
  595.       break;
  596.     case X (OP_REG, SL):
  597.       SET_L_REG (rn, n);
  598.       break;
  599.  
  600.     case X (OP_DEC, SB):
  601.       t = GET_L_REG (rn) - 1;
  602.       t &= cpu.mask;
  603.       SET_L_REG (rn, t);
  604.       SET_MEMORY_B (t, n);
  605.  
  606.       break;
  607.     case X (OP_DEC, SW):
  608.       t = (GET_L_REG (rn) - 2) & cpu.mask;
  609.       SET_L_REG (rn, t);
  610.       SET_MEMORY_W (t, n);
  611.       break;
  612.  
  613.     case X (OP_DEC, SL):
  614.       t = (GET_L_REG (rn) - 4) & cpu.mask;
  615.       SET_L_REG (rn, t);
  616.       SET_MEMORY_L (t, n);
  617.       break;
  618.  
  619.  
  620.     case X (OP_DISP, SB):
  621.       t = GET_L_REG (rn) + abs;
  622.       t &= cpu.mask;
  623.       SET_MEMORY_B (t, n);
  624.       break;
  625.  
  626.     case X (OP_DISP, SW):
  627.       t = GET_L_REG (rn) + abs;
  628.       t &= cpu.mask;
  629.       SET_MEMORY_W (t, n);
  630.       break;
  631.  
  632.     case X (OP_DISP, SL):
  633.       t = GET_L_REG (rn) + abs;
  634.       t &= cpu.mask;
  635.       SET_MEMORY_L (t, n);
  636.       break;
  637.     default:
  638.       abort ();
  639.     }
  640. }
  641.  
  642.  
  643. static union
  644. {
  645.   short int i;
  646.   struct
  647.     {
  648.       char low;
  649.       char high;
  650.     }
  651.   u;
  652. }
  653.  
  654. littleendian;
  655.  
  656. static
  657. void
  658. init_pointers ()
  659. {
  660.   static int init;
  661.  
  662.   if (!init)
  663.     {
  664.       int i;
  665.  
  666.       init = 1;
  667.       littleendian.i = 1;
  668.  
  669.       cpu.memory = (unsigned char *) calloc (sizeof (char), MSIZE);
  670.       cpu.cache_idx = (unsigned short *) calloc (sizeof (short), MSIZE);
  671.       cpu.mask = (1 << MPOWER) - 1;
  672.  
  673.       for (i = 0; i < 9; i++)
  674.     {
  675.       cpu.regs[i] = 0;
  676.     }
  677.  
  678.       for (i = 0; i < 8; i++)
  679.     {
  680.       unsigned char *p = (unsigned char *) (cpu.regs + i);
  681.       unsigned char *e = (unsigned char *) (cpu.regs + i + 1);
  682.       unsigned short *q = (unsigned short *) (cpu.regs + i);
  683.       unsigned short *u = (unsigned short *) (cpu.regs + i + 1);
  684.       cpu.regs[i] = 0x00112233;
  685.       while (p < e)
  686.         {
  687.           if (*p == 0x22)
  688.         {
  689.           breg[i] = p;
  690.         }
  691.           if (*p == 0x33)
  692.         {
  693.           breg[i + 8] = p;
  694.         }
  695.           p++;
  696.         }
  697.       while (q < u)
  698.         {
  699.           if (*q == 0x2233)
  700.         {
  701.           wreg[i] = q;
  702.         }
  703.           if (*q == 0x0011)
  704.         {
  705.           wreg[i + 8] = q;
  706.         }
  707.           q++;
  708.         }
  709.       cpu.regs[i] = 0;
  710.       lreg[i] = &cpu.regs[i];
  711.     }
  712.  
  713.  
  714.       lreg[8] = &cpu.regs[8];
  715.  
  716.       /* initialize the seg registers */
  717.       if (!cpu.cache)
  718.     sim_csize (CSIZE);
  719.  
  720.     }
  721. }
  722.  
  723. static void
  724. control_c (sig, code, scp, addr)
  725.      int sig;
  726.      int code;
  727.      char *scp;
  728.      char *addr;
  729. {
  730.   cpu.exception = SIGINT;
  731. }
  732.  
  733. #define C (c != 0)
  734. #define Z (nz == 0)
  735. #define V (v != 0)
  736. #define N (n != 0)
  737.  
  738. static int
  739. mop (code, bsize, sign)
  740.      decoded_inst *code;
  741.      int bsize;
  742.      int sign;
  743. {
  744.   int multiplier;
  745.   int multiplicand;
  746.   int result;
  747.   int n, nz;
  748.   if (sign)
  749.     {
  750.       multiplicand =
  751.     bsize ? SEXTCHAR (GET_W_REG (code->dst.reg)) :
  752.     SEXTSHORT (GET_W_REG (code->dst.reg));
  753.       multiplier =
  754.     bsize ? SEXTCHAR (GET_B_REG (code->src.reg)) :
  755.     SEXTSHORT (GET_W_REG (code->src.reg));
  756.     }
  757.   else
  758.     {
  759.       multiplicand = bsize ? UEXTCHAR (GET_W_REG (code->dst.reg)) :
  760.     UEXTSHORT (GET_W_REG (code->dst.reg));
  761.       multiplier =
  762.     bsize ? UEXTCHAR (GET_B_REG (code->src.reg)) :
  763.     UEXTSHORT (GET_W_REG (code->src.reg));
  764.  
  765.     }
  766.   result = multiplier * multiplicand;
  767.  
  768.   if (sign)
  769.     {
  770.       n = result & (bsize ? 0x8000 : 0x80000000);
  771.       nz = result & (bsize ? 0xffff : 0xffffffff);
  772.     }
  773.   if (bsize)
  774.     {
  775.       SET_W_REG (code->dst.reg, result);
  776.     }
  777.   else
  778.     {
  779.       SET_L_REG (code->dst.reg, result);
  780.     }
  781. /*  return ((n==1) << 1) | (nz==1); */
  782.  
  783. }
  784.  
  785. #define OSHIFTS(name, how) \
  786. case O(name, SB):                \
  787. {                        \
  788.   int t;                    \
  789.   int hm = 0x80;                \
  790.   rd = GET_B_REG (code->src.reg);        \
  791.   how;                         \
  792.   goto shift8;                    \
  793. }                         \
  794. case O(name, SW):                \
  795. {                         \
  796.   int t;                    \
  797.   int hm = 0x8000;                \
  798.   rd = GET_W_REG (code->src.reg);         \
  799.   how;                         \
  800.   goto shift16;                    \
  801. }                         \
  802. case O(name, SL):                \
  803. {                        \
  804.   int t;                    \
  805.   int hm = 0x80000000;                 \
  806.   rd = GET_L_REG (code->src.reg);        \
  807.   how;                         \
  808.   goto shift32;                    \
  809. }
  810.  
  811. #define OBITOP(name,f, s, op)             \
  812. case  O(name, SB):                \
  813. {                        \
  814.   int m;                    \
  815.   int b;                     \
  816.   if (f) ea = fetch (&code->dst);        \
  817.   m=1<< fetch(&code->src);            \
  818.   op;                        \
  819.   if(s) store (&code->dst,ea); goto next;    \
  820. }
  821.  
  822. int
  823. sim_resume (step, siggnal)
  824. {
  825.   static int init1;
  826.   int cycles = 0;
  827.   int insts = 0;
  828.   int tick_start = get_now ();
  829.   void (*prev) ();
  830.   int poll_count = 0;
  831.   int res;
  832.   int tmp;
  833.   int rd;
  834.   int ea;
  835.   int bit;
  836.   int pc;
  837.   int c, nz, v, n;
  838.  
  839.   init_pointers ();
  840.  
  841.   prev = signal (SIGINT, control_c);
  842.  
  843.   if (step)
  844.     {
  845.       cpu.exception = SIGTRAP;
  846.     }
  847.   else
  848.     {
  849.       cpu.exception = 0;
  850.     }
  851.  
  852.   pc = cpu.pc;
  853.  
  854.   GETSR ();
  855.  
  856.   do
  857.     {
  858.       int cidx;
  859.       decoded_inst *code;
  860.  
  861.     top:
  862.       cidx = cpu.cache_idx[pc];
  863.       code = cpu.cache + cidx;
  864.  
  865.  
  866. #define ALUOP(STORE, NAME, HOW) \
  867.     case O(NAME,SB):  HOW; if(STORE)goto alu8;else goto just_flags_alu8;  \
  868.     case O(NAME, SW): HOW; if(STORE)goto alu16;else goto just_flags_alu16; \
  869.     case O(NAME,SL):  HOW; if(STORE)goto alu32;else goto just_flags_alu32;
  870.  
  871.  
  872. #define LOGOP(NAME, HOW) \
  873.     case O(NAME,SB): HOW; goto log8;\
  874.     case O(NAME, SW): HOW; goto log16;\
  875.     case O(NAME,SL): HOW; goto log32;
  876.  
  877.  
  878.  
  879. #if ADEBUG
  880.       if (debug)
  881.     {
  882.       printf ("%x %d %s\n", pc, code->opcode,
  883.           code->op ? code->op->name : "**");
  884.     }
  885.       cpu.stats[code->opcode]++;
  886.  
  887. #endif
  888.  
  889.       cycles += code->cycles;
  890.       insts++;
  891.       switch (code->opcode)
  892.     {
  893.     case 0:
  894.       /*
  895.        * This opcode is a fake for when we get to an
  896.        * instruction which hasnt been compiled
  897.        */
  898.       compile (pc);
  899.       goto top;
  900.       break;
  901.  
  902.  
  903.     case O (O_SUBX, SB):
  904.       rd = fetch (&code->dst);
  905.       ea = fetch (&code->src);
  906.       ea = -(ea + C);
  907.       res = rd + ea;
  908.       goto alu8;
  909.  
  910.     case O (O_ADDX, SB):
  911.       rd = fetch (&code->dst);
  912.       ea = fetch (&code->src);
  913.       ea = C + ea;
  914.       res = rd + ea;
  915.       goto alu8;
  916.  
  917. #define EA    ea = fetch(&code->src);
  918. #define RD_EA ea = fetch(&code->src); rd = fetch(&code->dst);
  919.  
  920.       ALUOP (1, O_SUB, RD_EA;
  921.          ea = -ea;
  922.          res = rd + ea);
  923.       ALUOP (1, O_NEG, EA;
  924.          ea = -ea;
  925.          rd = 0;
  926.          res = rd + ea);
  927.  
  928.     case O (O_ADD, SB):
  929.       rd = GET_B_REG (code->dst.reg);
  930.       ea = fetch (&code->src);
  931.       res = rd + ea;
  932.       goto alu8;
  933.     case O (O_ADD, SW):
  934.       rd = GET_W_REG (code->dst.reg);
  935.       ea = fetch (&code->src);
  936.       res = rd + ea;
  937.       goto alu16;
  938.     case O (O_ADD, SL):
  939.       rd = GET_L_REG (code->dst.reg);
  940.       ea = fetch (&code->src);
  941.       res = rd + ea;
  942.       goto alu32;
  943.  
  944.  
  945.       LOGOP (O_AND, RD_EA;
  946.          res = rd & ea);
  947.  
  948.       LOGOP (O_OR, RD_EA;
  949.          res = rd | ea);
  950.  
  951.       LOGOP (O_XOR, RD_EA;
  952.          res = rd ^ ea);
  953.  
  954.  
  955.     case O (O_MOV_TO_MEM, SB):
  956.       res = GET_B_REG (code->src.reg);
  957.       goto log8;
  958.     case O (O_MOV_TO_MEM, SW):
  959.       res = GET_W_REG (code->src.reg);
  960.       goto log16;
  961.     case O (O_MOV_TO_MEM, SL):
  962.       res = GET_L_REG (code->src.reg);
  963.       goto log32;
  964.  
  965.  
  966.     case O (O_MOV_TO_REG, SB):
  967.       res = fetch (&code->src);
  968.       SET_B_REG (code->dst.reg, res);
  969.       goto just_flags_log8;
  970.     case O (O_MOV_TO_REG, SW):
  971.       res = fetch (&code->src);
  972.       SET_W_REG (code->dst.reg, res);
  973.       goto just_flags_log16;
  974.     case O (O_MOV_TO_REG, SL):
  975.       res = fetch (&code->src);
  976.       SET_L_REG (code->dst.reg, res);
  977.       goto just_flags_log32;
  978.  
  979.  
  980.     case O (O_ADDS, SL):
  981.       SET_L_REG (code->dst.reg,
  982.              GET_L_REG (code->dst.reg)
  983.              + code->src.literal);
  984.  
  985.       goto next;
  986.  
  987.     case O (O_SUBS, SL):
  988.       SET_L_REG (code->dst.reg,
  989.              GET_L_REG (code->dst.reg)
  990.              - code->src.literal);
  991.       goto next;
  992.  
  993.     case O (O_CMP, SB):
  994.       rd = fetch (&code->dst);
  995.       ea = fetch (&code->src);
  996.       ea = -ea;
  997.       res = rd + ea;
  998.       goto just_flags_alu8;
  999.  
  1000.     case O (O_CMP, SW):
  1001.       rd = fetch (&code->dst);
  1002.       ea = fetch (&code->src);
  1003.       ea = -ea;
  1004.       res = rd + ea;
  1005.       goto just_flags_alu16;
  1006.  
  1007.     case O (O_CMP, SL):
  1008.       rd = fetch (&code->dst);
  1009.       ea = fetch (&code->src);
  1010.       ea = -ea;
  1011.       res = rd + ea;
  1012.       goto just_flags_alu32;
  1013.  
  1014.  
  1015.     case O (O_DEC, SB):
  1016.       rd = GET_B_REG (code->src.reg);
  1017.       ea = -1;
  1018.       res = rd + ea;
  1019.       SET_B_REG (code->src.reg, res);
  1020.       goto just_flags_inc8;
  1021.  
  1022.     case O (O_DEC, SW):
  1023.       rd = GET_W_REG (code->dst.reg);
  1024.       ea = -code->src.literal;
  1025.       res = rd + ea;
  1026.       SET_W_REG (code->dst.reg, res);
  1027.       goto just_flags_inc16;
  1028.  
  1029.     case O (O_DEC, SL):
  1030.       rd = GET_L_REG (code->dst.reg);
  1031.       ea = -code->src.literal;
  1032.       res = rd + ea;
  1033.       SET_L_REG (code->dst.reg, res);
  1034.       goto just_flags_inc32;
  1035.  
  1036.  
  1037.     case O (O_INC, SB):
  1038.       rd = GET_B_REG (code->src.reg);
  1039.       ea = 1;
  1040.       res = rd + ea;
  1041.       SET_B_REG (code->src.reg, res);
  1042.       goto just_flags_inc8;
  1043.  
  1044.     case O (O_INC, SW):
  1045.       rd = GET_W_REG (code->dst.reg);
  1046.       ea = code->src.literal;
  1047.       res = rd + ea;
  1048.       SET_W_REG (code->dst.reg, res);
  1049.       goto just_flags_inc16;
  1050.  
  1051.     case O (O_INC, SL):
  1052.       rd = GET_L_REG (code->dst.reg);
  1053.       ea = code->src.literal;
  1054.       res = rd + ea;
  1055.       SET_L_REG (code->dst.reg, res);
  1056.       goto just_flags_inc32;
  1057.  
  1058.  
  1059. #define GET_CCR(x) BUILDSR();x = cpu.ccr
  1060.  
  1061.     case O (O_ANDC, SB):
  1062.       GET_CCR (rd);
  1063.       ea = code->src.literal;
  1064.       res = rd & ea;
  1065.       goto setc;
  1066.  
  1067.     case O (O_ORC, SB):
  1068.       GET_CCR (rd);
  1069.       ea = code->src.literal;
  1070.       res = rd | ea;
  1071.       goto setc;
  1072.  
  1073.     case O (O_XORC, SB):
  1074.       GET_CCR (rd);
  1075.       ea = code->src.literal;
  1076.       res = rd ^ ea;
  1077.       goto setc;
  1078.  
  1079.  
  1080.     case O (O_BRA, SB):
  1081.       if (1)
  1082.         goto condtrue;
  1083.       goto next;
  1084.  
  1085.     case O (O_BRN, SB):
  1086.       if (0)
  1087.         goto condtrue;
  1088.       goto next;
  1089.  
  1090.     case O (O_BHI, SB):
  1091.       if ((C || Z) == 0)
  1092.         goto condtrue;
  1093.       goto next;
  1094.  
  1095.  
  1096.     case O (O_BLS, SB):
  1097.       if ((C || Z))
  1098.         goto condtrue;
  1099.       goto next;
  1100.  
  1101.     case O (O_BCS, SB):
  1102.       if ((C == 1))
  1103.         goto condtrue;
  1104.       goto next;
  1105.  
  1106.     case O (O_BCC, SB):
  1107.       if ((C == 0))
  1108.         goto condtrue;
  1109.       goto next;
  1110.  
  1111.     case O (O_BEQ, SB):
  1112.       if (Z)
  1113.         goto condtrue;
  1114.       goto next;
  1115.     case O (O_BGT, SB):
  1116.       if (((Z || (N ^ V)) == 0))
  1117.         goto condtrue;
  1118.       goto next;
  1119.  
  1120.  
  1121.     case O (O_BLE, SB):
  1122.       if (((Z || (N ^ V)) == 1))
  1123.         goto condtrue;
  1124.       goto next;
  1125.  
  1126.     case O (O_BGE, SB):
  1127.       if ((N ^ V) == 0)
  1128.         goto condtrue;
  1129.       goto next;
  1130.     case O (O_BLT, SB):
  1131.       if ((N ^ V))
  1132.         goto condtrue;
  1133.       goto next;
  1134.     case O (O_BMI, SB):
  1135.       if ((N))
  1136.         goto condtrue;
  1137.       goto next;
  1138.     case O (O_BNE, SB):
  1139.       if ((Z == 0))
  1140.         goto condtrue;
  1141.       goto next;
  1142.  
  1143.     case O (O_BPL, SB):
  1144.       if (N == 0)
  1145.         goto condtrue;
  1146.       goto next;
  1147.     case O (O_BVC, SB):
  1148.       if ((V == 0))
  1149.         goto condtrue;
  1150.       goto next;
  1151.     case O (O_BVS, SB):
  1152.       if ((V == 1))
  1153.         goto condtrue;
  1154.       goto next;
  1155.  
  1156.     case O (O_SYSCALL, SB):
  1157.       printf ("%c", cpu.regs[2]);
  1158.       goto next;
  1159.  
  1160.       OSHIFTS (O_NOT, rd = ~rd);
  1161.       OSHIFTS (O_SHLL, c = rd & hm;
  1162.            rd <<= 1);
  1163.       OSHIFTS (O_SHLR, c = rd & 1;
  1164.            rd = (unsigned int) rd >> 1);
  1165.       OSHIFTS (O_SHAL, c = rd & hm;
  1166.            rd <<= 1);
  1167.       OSHIFTS (O_SHAR, t = rd & hm;
  1168.            c = rd & 1;
  1169.            rd >>= 1;
  1170.            rd |= t;
  1171.         );
  1172.       OSHIFTS (O_ROTL, c = rd & hm;
  1173.            rd <<= 1;
  1174.            rd |= C);
  1175.       OSHIFTS (O_ROTR, c = rd & 1;
  1176.            rd = (unsigned int) rd >> 1;
  1177.            if (c) rd |= hm;);
  1178.       OSHIFTS (O_ROTXL, t = rd & hm;
  1179.            rd <<= 1;
  1180.            rd |= C;
  1181.            c = t;
  1182.         );
  1183.       OSHIFTS (O_ROTXR, t = rd & 1;
  1184.            rd = (unsigned int) rd >> 1;
  1185.            if (C) rd |= hm; c = t;);
  1186.  
  1187.     case O (O_JMP, SB):
  1188.       {
  1189.         pc = fetch (&code->src);
  1190.         goto end;
  1191.  
  1192.       }
  1193.  
  1194.     case O (O_JSR, SB):
  1195.       {
  1196.         int tmp;
  1197.         pc = fetch (&code->src);
  1198.       call:
  1199.         tmp = cpu.regs[7];
  1200.  
  1201.         if (h8300hmode)
  1202.           {
  1203.         tmp -= 4;
  1204.         SET_MEMORY_L (tmp, code->next_pc);
  1205.           }
  1206.         else
  1207.           {
  1208.         tmp -= 2;
  1209.         SET_MEMORY_W (tmp, code->next_pc);
  1210.           }
  1211.         cpu.regs[7] = tmp;
  1212.  
  1213.         goto end;
  1214.       }
  1215.     case O (O_BSR, SB):
  1216.       pc = code->src.literal;
  1217.       goto call;
  1218.  
  1219.     case O (O_RTS, SB):
  1220.       {
  1221.         int tmp;
  1222.  
  1223.  
  1224.         tmp = cpu.regs[7];
  1225.  
  1226.         if (h8300hmode)
  1227.           {
  1228.         pc = GET_MEMORY_L (tmp);
  1229.         tmp += 4;
  1230.  
  1231.           }
  1232.         else
  1233.           {
  1234.         pc = GET_MEMORY_W (tmp);
  1235.         tmp += 2;
  1236.           }
  1237.  
  1238.         cpu.regs[7] = tmp;
  1239.         goto end;
  1240.       }
  1241.  
  1242.     case O (O_ILL, SB):
  1243.       cpu.exception = SIGILL;
  1244.       goto end;
  1245.  
  1246.     case O (O_BPT, SB):
  1247.       cpu.exception = SIGTRAP;
  1248.       goto end;
  1249.  
  1250.       OBITOP (O_BNOT, 1, 1, ea ^= m);
  1251.       OBITOP (O_BTST, 1, 0, nz = ea & m);
  1252.       OBITOP (O_BCLR, 1, 1, ea &= ~m);
  1253.       OBITOP (O_BSET, 1, 1, ea |= m);    
  1254.       OBITOP (O_BLD, 1, 0, c = ea & m);
  1255.       OBITOP (O_BILD, 1, 0, c = !(ea & m));
  1256.       OBITOP (O_BST, 1, 1, ea &= ~m;
  1257.           if (C) ea |= m);
  1258.       OBITOP (O_BIST, 1, 1, ea &= ~m;
  1259.           if (!C) ea |= m);
  1260.       OBITOP (O_BAND, 1, 0, c = (ea & m) && C);
  1261.       OBITOP (O_BIAND, 1, 0, c = !(ea & m) && C);
  1262.       OBITOP (O_BOR, 1, 0, c = (ea & m) || C);
  1263.       OBITOP (O_BIOR, 1, 0, c = !(ea & m) || C);
  1264.       OBITOP (O_BXOR, 1, 0, c = (ea & m) != C);
  1265.       OBITOP (O_BIXOR, 1, 0, c = !(ea & m) != C);
  1266.  
  1267.  
  1268. #define MOP(bsize, signed) mop(code, bsize,signed); goto next;
  1269.  
  1270.     case O (O_MULS, SB):
  1271.       MOP (1, 1);
  1272.       break;
  1273.     case O (O_MULS, SW):
  1274.       MOP (0, 1);
  1275.       break;
  1276.     case O (O_MULU, SB):
  1277.       MOP (1, 0);
  1278.       break;
  1279.     case O (O_MULU, SW):
  1280.       MOP (0, 0);
  1281.       break;
  1282.  
  1283.  
  1284.     case O (O_DIVU, SB):
  1285.       {
  1286.  
  1287.         rd = GET_W_REG (code->dst.reg);
  1288.         ea = GET_B_REG (code->src.reg);
  1289.         if (ea)
  1290.           {
  1291.         tmp = rd % ea;
  1292.         rd = rd / ea;
  1293.  
  1294.           }
  1295.         SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8));
  1296.         n = ea & 0x80;
  1297.         nz = ea & 0xff;
  1298.  
  1299.         goto next;
  1300.       }
  1301.     case O (O_DIVU, SW):
  1302.       {
  1303.  
  1304.         rd = GET_L_REG (code->dst.reg);
  1305.         ea = GET_W_REG (code->src.reg);
  1306.         n = ea & 0x8000;
  1307.         nz = ea & 0xffff;
  1308.         if (ea)
  1309.           {
  1310.         tmp = rd % ea;
  1311.         rd = rd / ea;
  1312.  
  1313.           }
  1314.         SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16));
  1315.         goto next;
  1316.       }
  1317.  
  1318.  
  1319.  
  1320.     case O (O_DIVS, SB):
  1321.       {
  1322.  
  1323.         rd = SEXTSHORT (GET_W_REG (code->dst.reg));
  1324.         ea = SEXTCHAR (GET_B_REG (code->src.reg));
  1325.         if (ea)
  1326.           {
  1327.  
  1328.         tmp = (int) rd % (int) ea;
  1329.         rd = (int) rd / (int) ea;
  1330.         n = rd & 0x8000;
  1331.         nz = 1;
  1332.  
  1333.           }
  1334.         else
  1335.           nz = 0;
  1336.         SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8));
  1337.         goto next;
  1338.       }
  1339.     case O (O_DIVS, SW):
  1340.       {
  1341.  
  1342.         rd = GET_L_REG (code->dst.reg);
  1343.         ea = SEXTSHORT (GET_W_REG (code->src.reg));
  1344.         if (ea)
  1345.           {
  1346.  
  1347.         tmp = (int) rd % (int) ea;
  1348.         rd = (int) rd / (int) ea;
  1349.         n = rd & 0x80000000;
  1350.         nz = 1;
  1351.  
  1352.           }
  1353.         else
  1354.           nz = 0;
  1355.         SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16));
  1356.         goto next;
  1357.       }
  1358.     case O (O_EXTS, SW):
  1359.       rd = GET_B_REG (code->src.reg + 8) & 0xff;    /* Yes, src, not dst.  */
  1360.       ea = rd & 0x80 ? -256 : 0;
  1361.       res = rd + ea;
  1362.       goto log16;
  1363.     case O (O_EXTS, SL):
  1364.       rd = GET_W_REG (code->src.reg) & 0xffff;
  1365.       ea = rd & 0x8000 ? -65536 : 0;
  1366.       res = rd + ea;
  1367.       goto log32;
  1368.     case O (O_EXTU, SW):
  1369.       rd = GET_B_REG (code->src.reg + 8) & 0xff;
  1370.       ea = 0;
  1371.       res = rd + ea;
  1372.       goto log16;
  1373.     case O (O_EXTU, SL):
  1374.       rd = GET_W_REG (code->src.reg) & 0xffff;
  1375.       ea = 0;
  1376.       res = rd + ea;
  1377.       goto log32;
  1378.  
  1379.     case O (O_NOP, SB):
  1380.       goto next;
  1381.  
  1382.     default:
  1383.       cpu.exception = 123;
  1384.       goto end;
  1385.  
  1386.     }
  1387.       abort ();
  1388.  
  1389.     setc:
  1390.       cpu.ccr = res;
  1391.       GETSR ();
  1392.       goto next;
  1393.  
  1394.     condtrue:
  1395.       /* When a branch works */
  1396.       pc = code->src.literal;
  1397.       goto end;
  1398.  
  1399.       /* Set the cond codes from res */
  1400.     bitop:
  1401.  
  1402.       /* Set the flags after an 8 bit inc/dec operation */
  1403.     just_flags_inc8:
  1404.       n = res & 0x80;
  1405.       nz = res & 0xff;
  1406.       v = (rd & 0x7f) == 0x7f;
  1407.       goto next;
  1408.  
  1409.  
  1410.       /* Set the flags after an 16 bit inc/dec operation */
  1411.     just_flags_inc16:
  1412.       n = res & 0x8000;
  1413.       nz = res & 0xffff;
  1414.       v = (rd & 0x7fff) == 0x7fff;
  1415.       goto next;
  1416.  
  1417.  
  1418.       /* Set the flags after an 32 bit inc/dec operation */
  1419.     just_flags_inc32:
  1420.       n = res & 0x80000000;
  1421.       nz = res & 0xffffffff;
  1422.       v = (rd & 0x7fffffff) == 0x7fffffff;
  1423.       goto next;
  1424.  
  1425.  
  1426.     shift8:
  1427.       /* Set flags after an 8 bit shift op, carry set in insn */
  1428.       n = (rd & 0x80);
  1429.       v = 0;
  1430.       nz = rd & 0xff;
  1431.       SET_B_REG (code->src.reg, rd);
  1432.       goto next;
  1433.  
  1434.  
  1435.     shift16:
  1436.       /* Set flags after an 16 bit shift op, carry set in insn */
  1437.       n = (rd & 0x8000);
  1438.       v = 0;
  1439.       nz = rd & 0xffff;
  1440.  
  1441.       SET_W_REG (code->src.reg, rd);
  1442.       goto next;
  1443.  
  1444.     shift32:
  1445.       /* Set flags after an 32 bit shift op, carry set in insn */
  1446.       n = (rd & 0x80000000);
  1447.       v = 0;
  1448.       nz = rd & 0xffffffff;
  1449.       SET_L_REG (code->src.reg, rd);
  1450.       goto next;
  1451.  
  1452.     log32:
  1453.       store (&code->dst, res);
  1454.     just_flags_log32:
  1455.       /* flags after a 32bit logical operation */
  1456.       n = res & 0x80000000;
  1457.       nz = res & 0xffffffff;
  1458.       v = 0;
  1459.       goto next;
  1460.  
  1461.     log16:
  1462.       store (&code->dst, res);
  1463.     just_flags_log16:
  1464.       /* flags after a 16bit logical operation */
  1465.       n = res & 0x8000;
  1466.       nz = res & 0xffff;
  1467.       v = 0;
  1468.       goto next;
  1469.  
  1470.  
  1471.     log8:
  1472.       store (&code->dst, res);
  1473.     just_flags_log8:
  1474.       n = res & 0x80;
  1475.       nz = res & 0xff;
  1476.       v = 0;
  1477.       goto next;
  1478.  
  1479.     alu8:
  1480.       SET_B_REG (code->dst.reg, res);
  1481.     just_flags_alu8:
  1482.       n = res & 0x80;
  1483.       nz = res & 0xff;
  1484.       v = ((ea & 0x80) == (rd & 0x80)) && ((ea & 0x80) != (res & 0x80));
  1485.       c = (res & 0x100);
  1486.       goto next;
  1487.  
  1488.     alu16:
  1489.       SET_W_REG (code->dst.reg, res);
  1490.     just_flags_alu16:
  1491.       n = res & 0x8000;
  1492.       nz = res & 0xffff;
  1493.       v = ((ea & 0x8000) == (rd & 0x8000)) && ((ea & 0x8000) != (res & 0x8000));
  1494.       c = (res & 0x10000);
  1495.       goto next;
  1496.  
  1497.     alu32:
  1498.       SET_L_REG (code->dst.reg, res);
  1499.     just_flags_alu32:
  1500.       n = res & 0x80000000;
  1501.       nz = res & 0xffffffff;
  1502.       v = ((ea & 0x80000000) == (rd & 0x80000000))
  1503.     && ((ea & 0x80000000) != (res & 0x80000000));
  1504.       switch (code->opcode / 4)
  1505.     {
  1506.     case O_ADD:
  1507.       c = ((unsigned) res < (unsigned) rd) || ((unsigned) res < (unsigned) ea);
  1508.       break;
  1509.     case O_SUB:
  1510.     case O_CMP:
  1511.       c = (unsigned) rd < (unsigned) -ea;
  1512.       break;
  1513.     case O_NEG:
  1514.       c = res != 0;
  1515.       break;
  1516.     }
  1517.       goto next;
  1518.  
  1519.     next:;
  1520.       pc = code->next_pc;
  1521.  
  1522.     end:
  1523.       ;
  1524.       /*      if (cpu.regs[8] ) abort(); */
  1525.  
  1526. #ifdef __GO32__
  1527.       /* Poll after every 100th insn, */
  1528.       if (poll_count++ > 100)
  1529.     {
  1530.       poll_count = 0;
  1531.       if (kbhit ())
  1532.         {
  1533.           int c = getkey ();
  1534.           control_c ();
  1535.         }
  1536.     }
  1537. #endif
  1538.  
  1539.     }
  1540.   while (!cpu.exception);
  1541.   cpu.ticks += get_now () - tick_start;
  1542.   cpu.cycles += cycles;
  1543.   cpu.insts += insts;
  1544.  
  1545.   cpu.pc = pc;
  1546.   BUILDSR ();
  1547.  
  1548.   signal (SIGINT, prev);
  1549. }
  1550.  
  1551.  
  1552.  
  1553.  
  1554. int
  1555. sim_write (addr, buffer, size)
  1556.      SIM_ADDR addr;
  1557.      unsigned char *buffer;
  1558.      int size;
  1559. {
  1560.   int i;
  1561.  
  1562.   init_pointers ();
  1563.   if (addr < 0 || addr + size > MSIZE)
  1564.     return 0;
  1565.   for (i = 0; i < size; i++)
  1566.     {
  1567.       cpu.memory[addr + i] = buffer[i];
  1568.       cpu.cache_idx[addr + i] = 0;
  1569.     }
  1570.   return size;
  1571. }
  1572.  
  1573. int
  1574. sim_read (addr, buffer, size)
  1575.      SIM_ADDR addr;
  1576.      unsigned char *buffer;
  1577.      int size;
  1578. {
  1579.   init_pointers ();
  1580.   if (addr < 0 || addr + size > MSIZE)
  1581.     return 0;
  1582.   memcpy (buffer, cpu.memory + addr, size);
  1583.   return size;
  1584. }
  1585.  
  1586.  
  1587.  
  1588. #define R0_REGNUM    0
  1589. #define R1_REGNUM    1
  1590. #define R2_REGNUM    2
  1591. #define R3_REGNUM    3
  1592. #define R4_REGNUM    4
  1593. #define R5_REGNUM    5
  1594. #define R6_REGNUM    6
  1595. #define R7_REGNUM    7
  1596.  
  1597. #define SP_REGNUM       R7_REGNUM    /* Contains address of top of stack */
  1598. #define FP_REGNUM       R6_REGNUM    /* Contains address of executing
  1599.                        * stack frame */
  1600.  
  1601. #define CCR_REGNUM      8    /* Contains processor status */
  1602. #define PC_REGNUM       9    /* Contains program counter */
  1603.  
  1604. #define CYCLE_REGNUM    10
  1605. #define INST_REGNUM     11
  1606. #define TICK_REGNUM     12
  1607.  
  1608.  
  1609. int
  1610. sim_store_register (rn, value)
  1611.      int rn;
  1612.      unsigned char *value;
  1613. {
  1614.   int longval;
  1615.   int shortval;
  1616.   int intval;
  1617.   longval = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
  1618.   shortval = (value[0] << 8) | (value[1]);
  1619.   intval = h8300hmode ? longval : shortval;
  1620.  
  1621.   init_pointers ();
  1622.   switch (rn)
  1623.     {
  1624.     case PC_REGNUM:
  1625.       cpu.pc = intval;
  1626.       break;
  1627.     default:
  1628.       abort ();
  1629.     case R0_REGNUM:
  1630.     case R1_REGNUM:
  1631.     case R2_REGNUM:
  1632.     case R3_REGNUM:
  1633.     case R4_REGNUM:
  1634.     case R5_REGNUM:
  1635.     case R6_REGNUM:
  1636.     case R7_REGNUM:
  1637.       cpu.regs[rn] = intval;
  1638.       break;
  1639.     case CCR_REGNUM:
  1640.       cpu.ccr = intval;
  1641.       break;
  1642.     case CYCLE_REGNUM:
  1643.       cpu.cycles = longval;
  1644.       break;
  1645.  
  1646.     case INST_REGNUM:
  1647.       cpu.insts = longval;
  1648.       break;
  1649.  
  1650.     case TICK_REGNUM:
  1651.       cpu.ticks = longval;
  1652.       break;
  1653.     }
  1654.   return 0;
  1655. }
  1656.  
  1657. int
  1658. sim_fetch_register (rn, buf)
  1659.      int rn;
  1660.      unsigned char *buf;
  1661. {
  1662.   int v;
  1663.   int longreg = 0;
  1664.  
  1665.   init_pointers ();
  1666.  
  1667.   switch (rn)
  1668.     {
  1669.     default:
  1670.       abort ();
  1671.     case 8:
  1672.       v = cpu.ccr;
  1673.       break;
  1674.     case 9:
  1675.       v = cpu.pc;
  1676.       break;
  1677.     case R0_REGNUM:
  1678.     case R1_REGNUM:
  1679.     case R2_REGNUM:
  1680.     case R3_REGNUM:
  1681.     case R4_REGNUM:
  1682.     case R5_REGNUM:
  1683.     case R6_REGNUM:
  1684.     case R7_REGNUM:
  1685.       v = cpu.regs[rn];
  1686.       break;
  1687.     case 10:
  1688.       v = cpu.cycles;
  1689.       longreg = 1;
  1690.       break;
  1691.     case 11:
  1692.       v = cpu.ticks;
  1693.       longreg = 1;
  1694.       break;
  1695.     case 12:
  1696.       v = cpu.insts;
  1697.       longreg = 1;
  1698.       break;
  1699.     }
  1700.   if (h8300hmode || longreg)
  1701.     {
  1702.       buf[0] = v >> 24;
  1703.       buf[1] = v >> 16;
  1704.       buf[2] = v >> 8;
  1705.       buf[3] = v >> 0;
  1706.     }
  1707.   else
  1708.     {
  1709.       buf[0] = v >> 8;
  1710.       buf[1] = v;
  1711.     }
  1712.   return 0;
  1713. }
  1714.  
  1715. int
  1716. sim_trace ()
  1717. {
  1718.   return 0;
  1719. }
  1720.  
  1721. int
  1722. sim_stop_reason (reason, sigrc)
  1723.      enum sim_stop *reason;
  1724.      int *sigrc;
  1725. {
  1726.   *reason = sim_stopped;
  1727.   *sigrc = cpu.exception;
  1728.   return 0;
  1729. }
  1730.  
  1731. int
  1732. sim_set_pc (n)
  1733.      SIM_ADDR n;
  1734. {
  1735.   cpu.pc = n;
  1736.   return 0;
  1737. }
  1738.  
  1739.  
  1740. sim_csize (n)
  1741. {
  1742.   if (cpu.cache)
  1743.     free (cpu.cache);
  1744.   if (n < 2)
  1745.     n = 2;
  1746.   cpu.cache = (decoded_inst *) malloc (sizeof (decoded_inst) * n);
  1747.   memset (cpu.cache, 0, sizeof (decoded_inst) * n);
  1748.   cpu.csize = n;
  1749. }
  1750.  
  1751.  
  1752.  
  1753. int
  1754. sim_info (printf_fn, verbose)
  1755.      void (*printf_fn) ();
  1756.      int verbose;
  1757.  
  1758. {
  1759.   double timetaken = (double) cpu.ticks / (double) now_persec ();
  1760.   double virttime = cpu.cycles / 10.0e6;
  1761.  
  1762.  
  1763.   printf ("\n\n#instructions executed  %10d\n", cpu.insts);
  1764.   printf ("#cycles (v approximate) %10d\n", cpu.cycles);
  1765.   printf ("#real time taken        %10.4f\n", timetaken);
  1766.   printf ("#virtual time taked     %10.4f\n", virttime);
  1767.   if (timetaken != 0.0)
  1768.     printf ("#simulation ratio       %10.4f\n", virttime / timetaken);
  1769.   printf ("#compiles               %10d\n", cpu.compiles);
  1770.   printf ("#cache size             %10d\n", cpu.csize);
  1771.  
  1772.  
  1773. #ifdef ADEBUG
  1774.   if (verbose)
  1775.     {
  1776.       int i;
  1777.       for (i = 0; i < O_LAST; i++)
  1778.     {
  1779.       if (cpu.stats[i])
  1780.         printf ("%d: %d\n", i, cpu.stats[i]);
  1781.     }
  1782.     }
  1783. #endif
  1784.  
  1785.   return 0;
  1786. }
  1787.  
  1788. void
  1789. set_h8300h ()
  1790. {
  1791.   h8300hmode = 1;
  1792. }
  1793.  
  1794. int
  1795. sim_kill ()
  1796. {
  1797.   return 0;
  1798. }
  1799.  
  1800. sim_open (name)
  1801.      char *name;
  1802. {
  1803.   return 0;
  1804. }
  1805.  
  1806. sim_set_args (argv, env)
  1807.      char **argv;
  1808.      char **env;
  1809. {
  1810.   return 0;
  1811. }
  1812.